#coding:utf-8

_bin = ['0000', '0001', '0010', '0011',
        '0100', '0101', '0110', '0111',
        '1000', '1001', '1010', '1011',
        '1100', '1101', '1110', '1111',]

#千里眼车台校验和算法
def getCheckSum(bytes):
    """ getchecksum(bytes)==> int\n
        
        Get the Checksum of data between Vehicle and Device,\n
        Used to send data or check received data.\n
    """
    checksum = 0
    for number in bytes:
        checksum += number
        if checksum > 0xff:
            checksum -= 0xff
    if checksum == 0: checksum = 0x7f
    return checksum

#千里眼业务数据末尾校验码
def getBussCheckSum(bytes):
    """接收业务数据字符串,返回计算得到的校验和字符串"""
    checksum = 0
    for number in bytes:
        checksum += number
        if checksum > 0xff:
            checksum -= 0xff
    if checksum >= 0x80: checksum -= 0x80
    if checksum == 0x00: checksum = 0x7f
    return checksum

# 添加7e头尾
def add7e(bytes):
    if not 0x7e in bytes:
        return [0x7e] + bytes + [0x7e]
    else:
        raise ValueError, "0x7e in data"
    
# 除掉7e头尾
def remove7e(bytes):
    isRecording = False
    index = 0
    _datas = []
    while index < len(bytes):
        try:
            targ = bytes.index(0x7e, index)
            if isRecording:
                _datas.append(bytes[index: targ])
            isRecording = not isRecording
            index = targ + 1
        except ValueError:
            break
    return _datas, bytes[index+1:]
        
# 转义字符,7e转为7d+02, 7d转为7d+01
def convert7e(bytes):
    l = []
    for number in bytes:
        if number == 0x7d:
            l += [0x7d, 0x01]
        elif number == 0x7e:
            l += [0x7d, 0x02]
        else:
            l.append(number)
    return l

# 逆转义字符,7d+01转为7d,7d+02转为7e
def rollback7e(bytes):
    index = 0
    while True:
        try:
            targ = bytes.index(0x7d, index)
            suffix = targ+1
            if bytes[suffix] == 0x02:
                bytes[targ] = 0x7e
            elif bytes[suffix] != 0x01:
                raise IndexError
            bytes.pop(suffix)
            index = suffix
        except ValueError:
            break
        except IndexError:
            raise IndexError, "can't convert 0x7d in %s"%targ
    return bytes
    
#数据x转八
def code_x_to_8(data, code):
    de = ''
    for i in data:
        de += (_bin[i/16] + _bin[i%16])[8-code:]
    if not de: return []
    de = de.rstrip('0')
    lst = []
    for i in range(len(de) / 8 + 1):
        lst.append(de[i * 8 :i * 8 + 8])
        
    #最后一个值需要补0
    lst[-1] = (lst[-1] + '0' * 8 )[:8]
        
    return [int(i,2) for i in lst]
    
#数据八转X
def code_8_to_x(data, code):
    de = ''
    for i in data:
        de += _bin[i/16] + _bin[i%16]
    if not de: return []
    lst = []
    for i in range(len(de) / code + 1):
        lst.append(de[i * code :i * code + code])
    
    #最后一个值需要补0
    lst[-1] = (lst[-1] + '0' * code )[:code]
    return [int(i, 2) for i in lst]